using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.IO.IsolatedStorage;

namespace WPCoco.Helpers
{
    public static class IsolatedStorageHelper
    {
        static IsolatedStorageFile storage;

        static IsolatedStorageHelper()
        {
            storage = IsolatedStorageFile.GetUserStoreForApplication();
        }

        public static bool CreateDirectory(string directoryName)
        {
            try
            {
                if (!string.IsNullOrEmpty(directoryName) && !storage.DirectoryExists(directoryName))
                    storage.CreateDirectory(directoryName);
                else
                    return false;
            }
            catch 
            {
                return false;
            }
            return true;
        }

        public static bool DeleteDirectory(string directoryName)
        {
            try
            {
                if (!string.IsNullOrEmpty(directoryName) && storage.DirectoryExists(directoryName))
                    storage.DeleteDirectory(directoryName);
                else
                    return false;
            }
            catch
            {
                return false;
            }
            return true;
        }

        public static bool DeleteDirectoryRecursive(string directoryName)
        {
            // TODO:
            return true;
        }

        public static bool ExistDirectory(string directoryName)
        {
            return storage.DirectoryExists(directoryName);
        }

        public static bool CreateFile(string filePath) 
        {
            try
            {
                IsolatedStorageFileStream stream = storage.CreateFile(filePath);
                stream.Close();
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }

        public static bool DeleteFile(string filePath) 
        {
            try
            {
                storage.DeleteFile(filePath);
            }
            catch 
            {
                return false;
            }
            return true;
        }

        public static bool ExistFile(string filePath) 
        {
            return storage.FileExists(filePath);
        }

        public static FileStream OpenRead(string filePath) 
        {
            IsolatedStorageFileStream stream = null;
            try
            {
                stream = new IsolatedStorageFileStream(filePath, FileMode.Open, storage);
            }
            catch { }
            return stream;
        }

        public static FileStream OpenWrite(string filePath) 
        {
            IsolatedStorageFileStream stream = null;
            try
            {
                stream = new IsolatedStorageFileStream(filePath, FileMode.Create, storage);
            }
            catch { }
            return stream;
        }
    }
}
